package com.info.zhiduoduo.repository.dto.crowdsource;

import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.common.utils.ImgBase64Util;
import com.info.zhiduoduo.repository.dto.company.CompanyDetailDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceCompanyParty;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProject;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProjectItem;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString
public class CrowdsourceContractDto {

	/**
	 * 子项目id
	 */
	private Integer id;
	/**
	 * 大项目id
	 */
	private Integer projectid;
	/**
	 * 1 自然人 2 创客
	 */
	private int userType;

	/**
	 * 创客或自然人主键
	 */
	private int userid;

	/**
	 * 甲方
	 */
	private String compname;

	/**
	 * 甲方电话
	 */
	private String companyPhone;

	/**
	 * 企业运营主体id
	 */
	private Integer partyId;

	/**
	 * 企业运营主体名称
	 */
	private String partyName;

	/**
	 * 乙方姓名，创客或自然人真实姓名
	 */
	private String userName;

	/**
	 * 乙方工作室名称
	 */
	private String studioName;

	/**
	 * 项目名称
	 */
	private String projectName;
	/**
	 * 项目描述
	 */
	private String projectDesc;

	/**
	 * 交易结束时间
	 */
	private Date finishtime;

	/**
	 * 共享经济服务费
	 */
	private BigDecimal projectAmount;

	/**
	 * 项目服务费+综合税费
	 */
	private BigDecimal projectServiceCharge;

	/**
	 * 项目创建时间
	 */
	private Date projectCreateTime;

	//合同编号
	private String contractNo;

	/**
	 * 智零工项目类型(1:智众包，2:智零工)
	 */
	private Integer workType;

	/**
	 * 收件人
	 */
	@ApiModelProperty("收件人")
	private String realname;

	/**
	 * 收件人电话
	 */
	private String addresseePhone;
	/**
	 * 企业地址
	 */
	private String companyAddress;

	private String idno;
	private String phone;
	private String acctno;
	private String bankname;

	private Integer province;
	private Integer city;

	public static CrowdsourceContractDto of(Company company, CrowdsourceCompanyParty party, CrowdsourceProject crowdsourceProject,
											CrowdsourceProjectItem projectItem) {

		CrowdsourceContractDto dto = new CrowdsourceContractDto();

		// 项目信息
		dto.setProjectid(crowdsourceProject.getProjectid())
				.setId(projectItem.getId())
				.setProjectName(crowdsourceProject.getProjectName())
				.setProjectDesc(crowdsourceProject.getProjectDesc())
				.setFinishtime(crowdsourceProject.getFinishtime())
				.setProjectCreateTime(crowdsourceProject.getCreatetime())
				.setProjectAmount(projectItem.getProjectAmount())
				.setProjectServiceCharge(projectItem.getProjectServiceCharge());

		// 企业绑定的主体信息
		dto.setCompname(company.getCompname()).setCompanyPhone(company.getContactphone());
		if (party != null) {
			dto.setPartyId(party.getId()).setPartyName(party.getCompanyName());
		}

		// 创客或自然人信息
		dto.setUserid(projectItem.getUserid())
				.setUserType(projectItem.getUserType())
				.setUserName(projectItem.getRealname())
				.setStudioName(projectItem.getUsername());

		return dto;
	}

	public static Map<String, Object> ofMap(CrowdsourceContractDto dto) {
		LocalDate localDate = DateUtil.date2LocalDate(dto.getProjectCreateTime()).plusYears(1).minusDays(1);
		Date endDate = DateUtil.localDate2Date(localDate);
		Map<String, Object> data = new HashMap<>();
		data.put("contractNo", dto.getContractNo());
		data.put("compname", dto.getPartyName());
		data.put("partyName", dto.getPartyName());
		data.put("username", dto.getStudioName());
		data.put("serviceCharge", dto.getProjectAmount());
		data.put("contractDate", DateUtil.getString(dto.getProjectCreateTime(), DateUtil.FORMAT_STYLE_3));
		data.put("validity", 1);
		data.put("startYear", DateUtil.getString(dto.getProjectCreateTime(), "yyyy"));
		data.put("startMonth", DateUtil.getString(dto.getProjectCreateTime(), "MM"));
		data.put("startDay", DateUtil.getString(dto.getProjectCreateTime(), "dd"));
		data.put("endYear", DateUtil.getString(endDate, "yyyy"));
		data.put("endMonth", DateUtil.getString(endDate, "MM"));
		data.put("endDay", DateUtil.getString(endDate, "dd"));
		data.put("projectname", dto.getProjectName());
		data.put("content", dto.getProjectDesc());
		data.put("addressee",dto.getRealname());
		data.put("addresseePhone",dto.getAddresseePhone());
		data.put("companyAddress",dto.getCompanyAddress());
		data.put("idno",dto.getIdno());
		data.put("phone",dto.getPhone());
		data.put("acctno",dto.getAcctno());
		data.put("bankname",dto.getBankname());
		return data;
	}

}
